package app.netant.galgame.galend;

import java.io.IOException;

import cn.lily.com.db.container.reader.DataTable;
import cn.lily.com.db.sql.DBManager;
import cn.lily.com.db.sql.manager.DBOfflineManager;
import cn.lily.com.enviroment.Enviroment;
import cn.lily.com.file.excel.io.ExcelWriter;
import cn.lily.com.file.io.FileInputManager;
import cn.lily.com.file.io.FileOutputManager;
import cn.lily.com.lang.Basic;


public class GalNetAnt{

	private void fileRelase() throws IOException{
		
		FileInputManager fim = new FileInputManager();
		fim.loadFile("D:/dd_bak.txt");
		
		FileOutputManager fom = new FileOutputManager();
		fom.loadFile("D:/dd_new.txt");
		fom.resetFile();
		
		String line = fim.readLine();
		boolean write = true;
		
		while(line != null){
			
			if(write == true && !"".equals(line.trim())){
				fom.writeLine(line);
				write = false;
			}
			
			if("".equals(line.trim())){
				write = true;
			}
			
			line = fim.readLine();
			
		}
		
		fom.free();
		fim.free();
		
	}
	
	private void fileCut() throws IOException{
		
		FileInputManager fim = new FileInputManager();
		fim.loadFile("D:/dd_new.txt");
		
		FileOutputManager fom = new FileOutputManager();
		fom.loadFile("D:/dd_new_cut.txt");
		fom.resetFile();
		
		String line = fim.readLine();
		
		while(line != null){
			
			int startIdx = line.lastIndexOf("】");
			
			int endIdx = line.indexOf("attach_img");
			
			if(endIdx == -1){
				endIdx = line.indexOf("attachment");
			}
			
			System.out.println(line);
			
			String cutLine = line.substring(startIdx+1,endIdx).trim();
			
			fom.writeLine(cutLine);
			
			line = fim.readLine();
			
		}
		
		fom.free();
		fim.free();
		
	}
	
	public void filePize() throws IOException {
		
		DBManager dbManager = DBOfflineManager.getDBManager();		
		ExcelWriter ew = new ExcelWriter("D:/dd.xlsx");	
		ew.inst();
		ew.locateSheet(0);
		FileInputManager fim = new FileInputManager();
		fim.loadFile("D:/dd_new_cut.txt");
		
		String line = fim.readLine();
		int rowIdx = 0;
		
		while(line != null){
			
			ew.locateRow(rowIdx);
			ew.setCellValue(0,line);
			
			if(line.indexOf("(")>0){
				line = line.substring(0,line.indexOf("("));
			}
			
			else if(line.indexOf("（")>0){
				line = line.substring(0,line.indexOf("（"));
			}

			System.out.println(line);
			line = line.replaceAll("'", "").replaceAll("\"", "");
			
			String sqlString = "select RES_ID,GAME_ID,GAME_NAME,GAME_TYP,GAME_LANG,"
					+ "GAME_VISUAL,GAME_SIZE,GAME_SIZE_NUM,GAME_FACTORY,GAME_FIRST,"
					+ "GAME_INTRU,GEME_MEMO,GAME_NAME_PLUS,"
					+ "DISK_ID,GAME_STORE_PATH,GAME_TAGS,"
					+ "to_char(GAME_PRO_TIME,'yyyy-mm-dd hh24:mi:ss') GAME_PRO_TIME,"
					+ "to_char(GAME_CRE_TIME,'yyyy-mm-dd hh24:mi:ss') GAME_CRE_TIME "
			 + "from ${schema}.LEO_GALGAME_INFO "
			 + "where 1=1 and ( upper(GAME_NAME) like '" + Basic.toWhereLike(line.trim().toUpperCase()) + "' or upper(GAME_NAME_PLUS) like '" + Basic.toWhereLike(line.trim().toUpperCase()) + "') "
			 +" order by GAME_CRE_TIME desc";
			
			DataTable dataTable = dbManager.executeQueryTable(sqlString);
			
			String name = "";			
			while(dataTable.next()){
				name += ","+ dataTable.getString("GAME_NAME");
				if(dataTable.getString("GAME_NAME_PLUS") != null && !"".equals(dataTable.getString("GAME_NAME_PLUS") )){
					name +=","+dataTable.getString("GAME_NAME_PLUS");
				}
			}
			ew.setCellValue(1,name);
			
			
			rowIdx++;
			line = fim.readLine();
			
		}
		ew.write();
		
		fim.free();
		
		
	}
	
	public static void main(String[] args) throws Exception {
		
		Enviroment.createEnv();
		Enviroment.getEnv().getLogBean().beginLog();
		
		GalNetAnt ant = new GalNetAnt();
		//ant.fileRelase();
		//ant.fileCut();
		ant.filePize();
	}

}
